﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Game.Utils;
using Game.Facade;
using System.Text;
using Game.Kernel;
using Game.Entity.NativeWeb;
using Game.Entity.Accounts;
using Game.Entity.Treasure;

namespace Game.Web.RoomCard
{
    public partial class BuyByGold : UCPageBase
    {
        #region 继承属性

        protected override bool IsAuthenticatedUser
        {
            get
            {
                return true;
            }
        }

        #endregion

        protected int rate = 1;
        protected int maxNum = 0;

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                //获取登录信息
                UserTicketInfo user = userTicket;

                //获取游戏币信息
                GameScoreInfo score = FacadeManage.aideTreasureFacade.GetTreasureInfo2(user.UserID);
                long insure = score != null ? score.InsureScore : 0;
                lbInsure.Text = insure.ToString();

                //获取房卡信息
                UserRoomCard card = FacadeManage.aideTreasureFacade.GetUserRoomCard(user.UserID);
                lbRoomCard.Text = card != null ? card.RoomCard.ToString() : "0";

                //购买比例
                Entity.Platform.GameProperty prop = FacadeManage.aidePlatformFacade.GetGameProperty(501);
                if(prop != null)
                {
                    rate = Convert.ToInt32(prop.Gold);
                }
                maxNum = Convert.ToInt32(Convert.ToDecimal(insure) / rate);
            }
        }

        /// <summary>
        /// 确定购买
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //获取数据
            int num = CtrlHelper.GetInt(txtBuyNum, 0);
            string password = CtrlHelper.GetText(txtPassword);

            //数据验证
            if(num <= 0)
            {
                Show("抱歉，购买数量不正确");
                return;
            }
            if(string.IsNullOrEmpty(password))
            {
                Show("抱歉，银行密码不正确");
                return;
            }

            Message msg = FacadeManage.aideTreasureFacade.BuyRoomCardGeneral(userTicket.UserID, TextEncrypt.EncryptPassword(password), num, GameRequest.GetUserIP(), 1);
            if(msg.Success)
            {
                Response.Redirect("/RoomCard/Success.aspx?id=3");
            }
            else
            {
                Show(msg.Content);
            }
        }

        /// <summary>
        /// 增加页面标题
        /// </summary>
        protected override void AddHeaderTitle()
        {
            AddMetaTitle("游戏币购买房卡 - " + ApplicationSettings.Get("title"));
            AddMetaKeywords(ApplicationSettings.Get("keywords"));
            AddMetaDescription(ApplicationSettings.Get("description"));
        }
    }
}